package com.hint.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hint.mapper.ProductOrderMapper;
import com.hint.model.ProductOrderDO;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * hint无需分片键，而是直接人为指向库表，因此绕开了SQL解析过程，性能更高。
 * 具体指定哪个库表，这个可以根据个人需求自行定制
 *
 * 指定库表，请看aspect包下的HintAspect
 */
@RestController
public class ShardingHintController {

    @Autowired(required = false)
    private ProductOrderMapper productOrderMapper;

    @GetMapping("testSaveProductOrder")
    public void testSaveProductOrder(){

        Random random = new Random();
        ProductOrderDO productOrderDO = new ProductOrderDO();
        productOrderDO.setCreateTime(new Date());
        productOrderDO.setNickname("666");
        productOrderDO.setOutTradeNo(UUID.randomUUID().toString().substring(0,32));
        productOrderDO.setPayAmount(100.00);
        productOrderDO.setState("PAY");
        Long aLong = Long.valueOf(random.nextInt(50));
        productOrderDO.setUserId(aLong);
        productOrderMapper.insert(productOrderDO);

    }

    /**
     * hint策略查询， hintManager正常应该写在AOP
     */
    @GetMapping("testHintSelect")
    public void testHintSelect(){

        //对应的value只做查询，不做sql解析
        productOrderMapper.selectList(new QueryWrapper<ProductOrderDO>().eq("id", 66L));

    }
}
